package top.shen33.yang.controller;


import com.sun.org.apache.xpath.internal.operations.Mod;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
public class LoginController {



    @GetMapping("/login")
    public Object login(){

        ModelAndView mav=new ModelAndView("/login");
        return mav;
    }


    @RequestMapping("/home")
    public Object home(){

        ModelAndView mav=new ModelAndView("/home");
        return mav;
    }
    @RequestMapping("/loginFail")
    public Object loginFail(){

        ModelAndView mav=new ModelAndView("/loginFail");
        return mav;
    }
//    @RequestMapping("/next")
//    public Object next(String page){
//
//        ModelAndView mav=new ModelAndView("/"+page);
//        return mav;
//    }

    //登录
//    @GetMapping("/doLogin")
    @RequestMapping("/doLogin")
    public Object doLogin(String uid, String pwd){

//        ModelAndView view=new ModelAndView();
//
//        //添加用户认证信息
//        Subject subject = SecurityUtils.getSubject();
//        UsernamePasswordToken token = new UsernamePasswordToken(uid,pwd);
//        try{
//            //进行验证，这里可以捕获异常，然后返回对应信息
//            subject.login(token);
//        }
//        catch(Exception e){
////            return "login failed";
////            return new ModelAndView("/loginFail");
//             view.setViewName("redirect:loginFail");
//            return view;
//        }
//
////        return "login success";
//        view.setViewName("redirect:home");
////        return "redirect:/redirectPage";
////        return new ModelAndView("/home");
//
//        return view;


        ModelAndView view=new ModelAndView();

        //添加用户认证信息
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(uid,pwd);
        try{
            //进行验证，这里可以捕获异常，然后返回对应信息
            subject.login(token);
        }
        catch(Exception e){
//            return "login failed";
            return new ModelAndView("redirect:/loginFail");
//            view.setViewName("redirect:/next?page=loginFail");
//            return view;
        }

//        return "login success";
//        view.setViewName("redirect:/next?page=home");
//        return view;
//        return "redirect:/redirectPage";
        return new ModelAndView("redirect:/home");


    }



    @RequestMapping(value = "/index")
    public String index(){
        return "index";
    }

    //登出
    @RequestMapping(value = "/logout")
    public String logout(){
        return "logout";
    }

    //错误页面展示
    @GetMapping("/error")
    public String error(){
        return "error ok!";
    }



    @RequiresRoles("admin")
    @RequiresPermissions("create")
    @RequestMapping(value = "/create")
    public String create(){
        return "Create success!";
    }

    @RequiresPermissions("detail")
    @RequestMapping(value = "/detail")
    public String detail(){
        return "uid";
    }
}
